package cn.middle.application.config;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.middle.application.dto.DoorEvent;
import cn.middle.application.dto.EdgeDto;
import cn.middle.application.dto.EdgeServiceDto;
import cn.middle.application.entity.EdgeService;
import cn.middle.application.entity.Park;
import cn.middle.application.service.EdgeServiceService;
import cn.middle.application.service.ParkService;
import cn.middle.remote.Buffer;
import cn.middle.remote.config.ClientConfig;
import cn.middle.remote.enums.NodeType;
import cn.middle.remote.serialization.TypeGenericity;
import cn.middle.remote.user.NettyUserClient;
import cn.middle.remote.user.UserClient;
import cn.middle.remote.user.UserListener;


@Configuration
public class RemoteConfig {
	
	@Bean
	public UserClient create() {
		
		ClientConfig config = new ClientConfig();
		config.setActiveSSL(false);
		config.setConnectTimeout(5000);
		config.setUserType(NodeType.EDGE);
		//边缘服务中转服务站  交互服务----》鉴权列表中 中先创建 获取
		config.setClientId(2);  //身份标识
		config.setUsrname("appUser");   //  用户名称
		config.setPassword("appUser12456"); //密码
		config.setRemoteHost("127.0.0.1"); //中间服务器的IP
		config.setRemotePort(12039);
		
		UserClient client = new NettyUserClient();
		client.setConfig(config);
		client.setListener(listener());
		//client.serialization(null); 序列化处理器，默认
		client.start();
		
		return client;
	}
	
	private UserListener listener() {
		return new UserListener() {
			private final Logger logger = LoggerFactory.getLogger(this.getClass());
			@Override
			public void connectAfter(Integer clientId, boolean state) {
				logger.info("connectAfter --> {}" ,clientId);
				
			}

			@Override
			public void exception(Throwable e) {
				logger.error(e.getMessage(),e);
				
			}

			@Override
			public void connectLost(Integer clientId, int reson) {
				logger.info("connectLost --> {}" ,clientId);
				
			}

			@Override
			public void ack(String msgId) {
				logger.info("ack --> {}" ,msgId);
				
			}

			@Override
			public void login(int code, String message) {
				logger.info("login --> {},{}" ,code,message);
				
			}

			@Override
			public void reply(String msgId, String interfaceName, Buffer buffer) {
				logger.info("reply --> {},{}" ,msgId,interfaceName);
				
			}

			@Override
			public void subscribeEvent(int eventId, Buffer buffer) {
				logger.info("subscribeEvent --> {} ---> {} " ,eventId,buffer.to(new TypeGenericity<DoorEvent>(){}));
				
			}

			@Override
			public void edgeReply(Buffer buffer) {
				List<EdgeDto> dtos = buffer.to(new TypeGenericity<List<EdgeDto>>(){});
				for(EdgeDto dto :dtos) {
					Park park = new Park();
					park.setDescription(dto.getDescribe());
					park.setParkAddress(dto.getAddress());
					park.setParkCode(dto.getEdgeId());
					park.setRemoteId(dto.getClientId());
					park.setResiterTime(dto.getRegistryTime());
					park.setParkName(dto.getEdgeName());
					ParkService parkService = ApplicationContextUtil.getBean(ParkService.class);
					Park sysPark = parkService.byParkCode(dto.getEdgeId());
					if(sysPark == null) {
						parkService.save(park);
					}else {
						park.setId(sysPark.getId());
						parkService.update(park);
					}
					
					List<EdgeServiceDto> EdgeServiceDtos = dto.getServices();
					for(EdgeServiceDto serviceDto : EdgeServiceDtos) {
						EdgeService service = new EdgeService();
						service.setParkCode(dto.getEdgeId());
						service.setRegisterTime(new Date());
						service.setServiceId(serviceDto.getServiceId());
						service.setServiceName(serviceDto.getServiceName());
						service.setServiceType(serviceDto.getServiceType());
						service.setStatus(serviceDto.getStatus());
						service.setUpdateTime(new Date());		
						
						EdgeServiceService edgeService = ApplicationContextUtil.getBean(EdgeServiceService.class);
						EdgeService sysEdgeService = edgeService.byServiceId(serviceDto.getServiceId());
						if(sysEdgeService != null) {
							service.setId(sysEdgeService.getId());
							edgeService.update(service);
						}else {
							edgeService.save(service);
						}
					}
				}
			}};
	}
	
	
}
